package com.vc.audio;

import java.io.File;
import java.lang.reflect.Method;

import android.content.Context;
import android.media.AudioManager;
import android.os.Build;

public class AudioSettingManager {

	Context mCtxt;

	// default setting for our app.
	private boolean bSpeakIsOn = false;
	private boolean bMicIsMute = false;

	// default audio settings..
	private int nDefMode = AudioManager.MODE_NORMAL;
	private int nDefVoiceVolume = 0;
	public static int nDefMusicVolume = 0;
	private boolean bDefSpeakerIsOn = false;
	private boolean bDefMicrophoneIsMute = false;
	static int nUsedMode = AudioManager.MODE_NORMAL;
	static int nVolumeMode = AudioManager.STREAM_VOICE_CALL;

	static boolean bSwitchSpkEnable = false;

	// change flag.
	public static boolean bModeSet = false;

	public AudioSettingManager(Context context) {
		// TODO Auto-generated constructor stub
		mCtxt = context;
	}

	public boolean IsMotorolaPhone() {
		String manufacture = Build.MANUFACTURER;
		String brandName = Build.MODEL;

		// olyatt
		return (manufacture.contains("motorola")||brandName.contains("Milestone") || brandName.contains("MB860")|| brandName.contains("XT882") || brandName.contains("Nexus S"));
	
	}

	public boolean IsSamsungPhone() {
		String manufacture = Build.MANUFACTURER;
		String brandName = Build.MODEL;

		return (manufacture.contains("samsung")||brandName.contains("SAMSUNG") || brandName.contains("SPH-")
				|| brandName.contains("SGH-") || brandName.contains("GT-")
				|| brandName.contains("SCH-"));
	}
	
	public boolean IsSpecialSamsungPhone() {

		String brandName = Build.MODEL;	
		
		return (brandName.contains("GT-I9082")||brandName.contains("SCH-W2013")
			  ||brandName.contains("GT-B9388")||brandName.contains("SCH-I939D")
			  ||brandName.contains("GT-N7100 ")||brandName.contains("GT-N7102")
			  ||brandName.contains("GT-I8552")||brandName.contains("GT-I9260")
			  ||brandName.contains("GT-I9500")||brandName.contains("GT-I9502")
			  ||brandName.contains("SCH-I959")||brandName.contains("GT-I9508"));

		
		
	}

	public boolean IsCoolpad() {
		// brand and os version info.
		String manufacture = Build.MANUFACTURER;
		String brandName = Build.MODEL;

		return (brandName.contains("D530") || manufacture.contains("YuLong")||brandName.contains("8150"));
	}

	//ZTE,u880
	public boolean IsZTE() {
		// brand and os version info.
		String manufacture = Build.MANUFACTURER;
		//String brandName = Build.MODEL;

		return (manufacture.contains("ZTE"));
	}
     //Xiaomi,MI-ONE plus
	public boolean IsXiaomi() {
		// brand and os version info.
		String manufacture = Build.MANUFACTURER;
		//String brandName = Build.MODEL;

		return (manufacture.contains("Xiaomi"));
	}
    //SonyErrisin X10, specific.
	public boolean IsSonyErrison() {
		// brand and os version info.
		String manufacture = Build.MANUFACTURER;
		String brandName = Build.MODEL;

		return (brandName.contains("X10"));
	}
	
	//
	public boolean IsHTCOneX() {
		String manufacture = Build.MANUFACTURER;
		String brandName = Build.MODEL;
		return (manufacture.contains("HTC") && brandName.contains("HTC One X"));
	}
	
	//zhanxun 8810 platform.
    //decide:/proc/asound/sprdphone sys/bus/platform/drivers/sc8800g_vsp 
	public boolean IsSP8810()
	{
		File phoneDirFile  = new File("/proc/asound/sprdphone");
		File sc8800gVspDir = new File("/sys/bus/platform/drivers/sc8800g_vsp");
		File sc8800gVbcDir = new File("/sys/bus/platform/drivers/sc88xx-vbc");
		//File sc8800gVbcDir1 = new File("/sys/bus/platform/drivers/sc88xx_vbc");
		
		//if (phoneDirFile.exists()&& sc8800gVspDir.exists())
		if((phoneDirFile.isDirectory() && sc8800gVspDir.isDirectory()) 
		 ||(phoneDirFile.isDirectory() && sc8800gVbcDir.isDirectory()))
		{
           return true;
		}
		return false;
	}
	
	//
	public void SetAudioConfigForCall(boolean bIsSwitchSpkMode,int mode) {

		if (bModeSet)
			return;

		AudioManager am = (AudioManager) mCtxt
				.getSystemService(Context.AUDIO_SERVICE);

		nDefMode = am.getMode();
		bDefSpeakerIsOn = am.isSpeakerphoneOn();
		bDefMicrophoneIsMute = am.isMicrophoneMute();

		// set mode....
		if (/*IsSamsungPhone()||*/IsSpecialSamsungPhone()) {
			am.setMode(AudioManager.MODE_NORMAL);
			nUsedMode = AudioManager.MODE_NORMAL;
			
			//am.setMode(AudioManager.MODE_IN_CALL);
			//nUsedMode = AudioManager.MODE_IN_CALL):
			
		} else if (IsMotorolaPhone() || IsCoolpad() || IsSonyErrison() || IsHTCOneX()) {
			am.setMode(AudioManager.MODE_NORMAL);
			nUsedMode = AudioManager.MODE_NORMAL;
		} 
		else if(IsSP8810())
		{
			am.setMode(AudioManager.MODE_NORMAL);
			nUsedMode = AudioManager.MODE_NORMAL;
		}
		else {

			am.setMode(AudioManager.MODE_IN_CALL);
			nUsedMode = AudioManager.MODE_IN_CALL;
			 
			//am.setMode(AudioManager.MODE_NORMAL);
			//nUsedMode = AudioManager.MODE_NORMAL;
		}
	

		//mode =1;
		
		//According to server config set MODE.
		if(0 != mode)
		{
			//normal.
			if(1 == mode)
			{
				am.setMode(AudioManager.MODE_NORMAL);
				nUsedMode = AudioManager.MODE_NORMAL;

			}
			//voice call
			else if(2 == mode)
			{
				am.setMode(AudioManager.MODE_IN_CALL);
				nUsedMode = AudioManager.MODE_IN_CALL;

			}
			else
			;			 	
		}
		
		nDefVoiceVolume = am.getStreamVolume(AudioManager.STREAM_VOICE_CALL);
		nDefMusicVolume = am.getStreamVolume(AudioManager.STREAM_MUSIC);

		// SetSpeakerOn(false);
		SetMicrophoneMute(false);

		// has set audio mode for call.
		bModeSet = true;

		bSwitchSpkEnable = bIsSwitchSpkMode;
		if(true == bSwitchSpkEnable)
			nVolumeMode = AudioManager.STREAM_MUSIC;
		else
			nVolumeMode = AudioManager.STREAM_VOICE_CALL;
	}

	void ResumeAudioConfig() {
		if (!bModeSet)
			return;

		AudioManager am = (AudioManager) mCtxt
				.getSystemService(Context.AUDIO_SERVICE);

		am.setMode(nDefMode);

		//am.setStreamVolume(AudioManager.STREAM_MUSIC, nDefMusicVolume, 0);
		//am.setStreamVolume(AudioManager.STREAM_VOICE_CALL, nDefVoiceVolume, 0);

		am.setSpeakerphoneOn(bDefSpeakerIsOn);
		am.setMicrophoneMute(bDefMicrophoneIsMute);

		// resume default mode...
		bModeSet = false;
	}

	void SetMicrophoneMute(boolean bIsMute) {

		bMicIsMute = bIsMute;

		AudioManager am = (AudioManager) mCtxt
				.getSystemService(Context.AUDIO_SERVICE);

		am.setMicrophoneMute(bMicIsMute);

	}

	void SetSpeakerOn(boolean bIsOn) {
		bSpeakIsOn = bIsOn;

		AudioManager am = (AudioManager) mCtxt
				.getSystemService(Context.AUDIO_SERVICE);

//		if (bSwitchSpkEnable) {
//			if (!bSpeakIsOn) {
//				am.setStreamVolume(AudioManager.STREAM_MUSIC,
//						am.getStreamMaxVolume(AudioManager.STREAM_MUSIC), 0);
//			} else {
//				int scaleVolume = 0;
//				scaleVolume = am.getStreamMaxVolume(AudioManager.STREAM_MUSIC) * 6 / 10;
//				am.setStreamVolume(AudioManager.STREAM_MUSIC, scaleVolume, 0);
//			}
//		} else {
//			am.setStreamVolume(AudioManager.STREAM_VOICE_CALL,
//					am.getStreamMaxVolume(AudioManager.STREAM_VOICE_CALL), 0);
//		}

		am.setSpeakerphoneOn(bSpeakIsOn);
	}
	
	void SetSpeakerVolume(int vol, int mode) {
		AudioManager am = (AudioManager) mCtxt
				.getSystemService(Context.AUDIO_SERVICE);

		int setVol = vol;
		if (vol > am.getStreamMaxVolume(mode)) {
			setVol = am.getStreamMaxVolume(mode);
		}
		
		am.setStreamVolume(mode, setVol, 0);
	}

	static int getUsedMode() {
		return nUsedMode;
	}

	static boolean isSwitchSpkModeEnable() {
		return bSwitchSpkEnable;
	}

	static int getVolumeMode() {
		return nVolumeMode;
		
	}
	public int getDeviceCurrentVolume(int mode) {
		AudioManager am = (AudioManager) mCtxt
				.getSystemService(Context.AUDIO_SERVICE);
		return am.getStreamVolume(mode);
	}

	public int getDeviceMaxVolume(int mode) {
		AudioManager am = (AudioManager) mCtxt
				.getSystemService(Context.AUDIO_SERVICE);
		return am.getStreamMaxVolume(mode);
	}

	static public boolean isHeadsetPluged(Context cxt) {
		boolean isHeadsetPluged;

		try {
			AudioManager am = (AudioManager) cxt
					.getSystemService(Context.AUDIO_SERVICE);
			Method isWiredHeadsetOn = am.getClass().getMethod(
					"isWiredHeadsetOn", new Class[] {});
			Object o = isWiredHeadsetOn.invoke(am, (Object[]) null);
			isHeadsetPluged = Boolean.parseBoolean(o.toString());

		} catch (Exception e) {
			isHeadsetPluged = false;
		}

		return isHeadsetPluged;
	}

	/* get the default music volume, added by slguo 2011-08-17 */
	public static int getSystemDefaultMusicVolume(Context _context) {
		if (bModeSet == false) {
			{
				AudioManager am = (AudioManager) _context
						.getSystemService(Context.AUDIO_SERVICE);

				return am.getStreamVolume(AudioManager.STREAM_MUSIC);
			}
		} else {
			return nDefMusicVolume;
		}
	}
}